package mobileMoney;

import java.awt.Color;
import java.awt.*;
import javax.swing.JFrame;
import sim.portrayal.*;
import sim.engine.*;
import sim.portrayal.Inspector;
import sim.portrayal.continuous.ContinuousPortrayal2D;
import sim.portrayal.network.NetworkPortrayal2D;
import sim.portrayal.network.SimpleEdgePortrayal2D;
import sim.portrayal.network.SpatialNetwork2D;
import sim.portrayal.simple.CircledPortrayal2D;
import sim.portrayal.simple.LabelledPortrayal2D;
import sim.portrayal.simple.MovablePortrayal2D;
import sim.portrayal.simple.OvalPortrayal2D;
import sim.display.*;

public class ClientsUI extends GUIState {
	public Display2D display;
	public JFrame displayFrame;
	ContinuousPortrayal2D landPortrayal = new ContinuousPortrayal2D();
	NetworkPortrayal2D contactsPortrayal = new NetworkPortrayal2D();

	public ClientsUI(SimState state) {
		super(state);
		// TODO Auto-generated constructor stub
	}

	public ClientsUI() {
		super(new Clients(System.currentTimeMillis()));
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		ClientsUI vid = new ClientsUI();
		Console c = new Console(vid);
		c.setVisible(true);
	}

	public static String getName() {
		return "Mobile Money Clients Sim";
	}

	public void start() {
		super.start();
		setupPortrayals();
	}

	public void load(SimState state) {
		super.load(state);
		setupPortrayals();
	}

	public void setupPortrayals() {
		Clients clients = (Clients) state;
		// tell the portrayals what to portray and how to portray them
		landPortrayal.setField(clients.land);
		landPortrayal.setPortrayalForAll(new MovablePortrayal2D(
				new CircledPortrayal2D(
						new LabelledPortrayal2D(new OvalPortrayal2D() {
							public void draw(Object object,
									Graphics2D graphics, DrawInfo2D info) {
								Client client = (Client) object;
								double active = 1;
								if (client.active==false) active=0;
								int locShade = (int) (active *80*client.profile.id);
								if (client.fraudster) locShade = 255;
								if (locShade > 255)
									locShade = 255;
								paint = new Color(locShade, 0, 255 - locShade);
								super.draw(object, graphics, info);
							}
						}, 5.0, null, Color.black, true), 0, 5.0, Color.green,
						true)));

		contactsPortrayal.setField(new SpatialNetwork2D(clients.land,
				clients.contacts));
		contactsPortrayal.setPortrayalForAll(new SimpleEdgePortrayal2D());
		// reschedule the displayer
		display.reset();
		display.setBackdrop(Color.white);
		// redraw the display
		display.repaint();
	}

	public void init(Controller c) {
		super.init(c);
		display = new Display2D(600, 600, this);
		display.setClipping(false);
		displayFrame = display.createFrame();
		displayFrame.setTitle("Mobile Money Display");
		c.registerFrame(displayFrame);
		// so the frame appears in the "Display" list
		displayFrame.setVisible(true);
		display.attach(landPortrayal, "Land");
		display.attach(contactsPortrayal, "Contacts");
	}

	public Object getSimulationInspectedObject() {
		return state;
	}

	public Inspector getInspector() {
		Inspector i = super.getInspector();
		i.setVolatile(true);
		return i;
	}

	public void quit() {
		super.quit();
		if (displayFrame != null)
			displayFrame.dispose();
		displayFrame = null;
		display = null;
	}

}
